// pages/environment/index.js
const app = getApp()
import * as echarts from '../..//components/ec-canvas/echarts';


function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    title: {},
    legend: {
      data: ['昨天', '今天'],
      top: 10,
      left: 'center',
      backgroundColor: 'white',
      z: 100
    },
    grid: {
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['0:00', '1:00', '2:00', '3:00', '4:00', '5:00', '6:00', '7:00', '8:00', '9:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00'],
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      // splitLine: {
      //   lineStyle: {
      //     type: 'dotted',
      //     color: '#002FA7'
      //   }
      // }
      // show: false
    },
    series: [{
      name: '实时曲线',
      type: 'line',
      smooth: true,
      data: [18, 36, 65, 30, 78, 40, 33, 36, 65, 30, 78, 40, 33, 36, 65, 30, 78, 40, 33, 36, 65, 30, 78, 40]
    }]
  };

  chart.setOption(option);
  return chart;
}

let _timer1 = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    device: null,
    deviceId: null,
    alarmContent:[],
    ec: {
      onInit: initChart
    },
    indicatorDots: true,
    vertical:true,
    autoplay: false,
    interval: 2000,
    duration: 500
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let deviceId = options.deviceId
    if (deviceId != null) {
      this.setData({
        deviceId: deviceId
      })
      app.api.getDeviceDetail(deviceId).then(res => {
        if (res.data != null) {
          this.setData({
            device: res.data,
            alarmContent: res.data.alarmContent
          })
          wx.setNavigationBarTitle({
            title: res.data.deviceName
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

    let _this = this
    _timer1 = setInterval(function () {
      if (_this.data.deviceId != null) {
        app.api.getDeviceDetail(_this.data.deviceId).then(res => {
          if (res.data != null) {
            _this.setData({
              device: res.data,
              alarmContent: res.data.alarmContent
            })
          }
        })
      }
    }, 2500)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    if (_timer1 != null) {
      clearInterval(_timer1)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
